#include <iostream>
#include <sstream>
#include <time.h>
#include <stdio.h>

#include "sysfs.h"
#include "sysarch.h"
#include "sysapi.h"
#include "board.h"
#include "sysdebug.h"
#include "Monitor.h"

#include "mvMat.h"
#include "OpencvIf.h"

#include "sysfs.h"
#include "sysarch.h"
#include "sysapi.h"
#include "board.h"
#include "sysdebug.h"
#include "Monitor.h"
#include "MenuIf.h"

#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>

#include "mv_camera_tools.hpp"
#include "mv_pub.hpp"
#include "para.hpp"

using namespace cv;
using namespace std;


void test()
{
	 Mat img(3, 4, CV_8UC3, Scalar_<uchar>(1, 2, 3));

	  cout << img << endl;

	  cout << "dims:" << img.dims << endl;
	  cout << "rows:" << img.rows << endl;
	  cout << "cols:" << img.cols << endl;
	  cout << "channels:" << img.channels() << endl;
	  cout << "type:" << img.type() << endl;
	  cout << "depth:" << img.depth() << endl;
	  cout << "elemSize:" << img.elemSize() << endl;
	  cout << "elemSize1:" << img.elemSize1() << endl;
	  cout << "step:" << img.step << endl;
}

TThreadRet Auto360Thread(void* pvPara)
{
	CmvMat view;
	CmvMat dst;

	COpencvIf opencvIf;
	TOpencvIfPara opencvIfPara;
	VIDEO_FRAME_INFO_S stFrame;

	Sleep(2000);

	int iMonitorID = ReqMonitor((char *)"main-thrd", 60 * 60);    //申请线程监控ID,更新间隔为10分钟

	Debug(DEBUG_CRITICAL, ("Auto360Thread : started!\r\n"));

	opencvIfPara.u32Width = 960;
	opencvIfPara.u32Height = 576;
	while ((!opencvIf.initOpencvIf(&opencvIfPara)))
	{
		printf("opencvIf init err!!!\r\n");
		Sleep(1000);
	}

	Size imageSize(opencvIfPara.u32Width, opencvIfPara.u32Height);
	dst.createImage(imageSize, CV_8UC3);
	view.createImage(imageSize, CV_8UC3);

	while (1)
	{
		ClrMonitorWdg(iMonitorID);
		Sleep(1000);
		//continue;

		HI_MPI_VI_GetFrame(0, &stFrame);
		HI_MPI_VO_SendFrame(SAMPLE_VO_DEV_DSD1, 1, &stFrame);
/*
		cout << "enPixelFormat:" << stFrame.stVFrame.enPixelFormat << endl;
		cout << "u32PhyAddr:" << stFrame.stVFrame.u32PhyAddr[0] << endl;
		cout << "u32PhyAddr:" << stFrame.stVFrame.u32PhyAddr[1] << endl;
		cout << "u32PhyAddr:" << stFrame.stVFrame.u32PhyAddr[2] << endl;

		cout << "u32Stride:" << stFrame.stVFrame.u32Stride[0] << endl;
		cout << "u32Stride:" << stFrame.stVFrame.u32Stride[1] << endl;
		cout << "u32Stride:" << stFrame.stVFrame.u32Stride[2] << endl;
*/

		opencvIf.colorConvertoBGR(&stFrame, &dst);


		printf("color conver 1!!!\r\n");
		cout << "dst.rows:" << dst.rows << endl;
		cout << "dst.cols:" << dst.cols << endl;
		cout << "dst.step:" << dst.step << endl;
/*
		for(int i=0; i<dst.rows; i++)
		{
			for(int j=0; j<dst.cols; j++)
			{
				dst.data[i*dst.step+j+0] = 255;
				dst.data[i*dst.step+j+1] = 255;
				dst.data[i*dst.step+j+2] = 255;
			}
		}
*/
		//printf("color conver 2!!!\r\n");

		//GenBmpFile((U8*)dst.data, 24, dst.cols, dst.rows, "./out.bmp");//生成BMP文件
/*
		cout << "enPixelFormat:" << stFrame.stVFrame.enPixelFormat << endl;
		cout << "u32PhyAddr:" << stFrame.stVFrame.u32PhyAddr[0] << endl;
		cout << "u32PhyAddr:" << stFrame.stVFrame.u32PhyAddr[1] << endl;
		cout << "u32PhyAddr:" << stFrame.stVFrame.u32PhyAddr[2] << endl;

		cout << "u32Stride:" << stFrame.stVFrame.u32Stride[0] << endl;
		cout << "u32Stride:" << stFrame.stVFrame.u32Stride[1] << endl;
		cout << "u32Stride:" << stFrame.stVFrame.u32Stride[2] << endl;
*/
		opencvIf.videoHiFrameToMat(&stFrame, &view);
		/*cout << "view.rows:" << view.rows << endl;
		cout << "view.cols:" << view.cols << endl;
		cout << "view.step" << view.step << endl;
		cout << "view.dims" << view.dims << endl;*/
		//cvtColor(view, dst, CV_YUV420sp2RGBA);

		HI_MPI_VI_ReleaseFrame(0, &stFrame);




		HI_MPI_VPSS_UserGetFrame(0, 0,	&stFrame);
		HI_MPI_VO_SendFrame(SAMPLE_VO_DEV_DSD1, 2, &stFrame);
		//HI_MPI_VPSS_UserReleaseFrame(0, 0, &stFrame);

/*
		cout << "dims:" << view.dims << endl;
		cout << "rows:" << view.rows << endl;
		cout << "cols:" << view.cols << endl;
		cout << "channels:" << view.channels() << endl;
		cout << "type:" << view.type() << endl;
		cout << "depth:" << view.depth() << endl;
		cout << "elemSize:" << view.elemSize() << endl;
		cout << "elemSize1:" << view.elemSize1() << endl;
		cout << "step:" << view.step << endl;
*/

		//MvPub mvPub;
		//mvPub.getCircleMapSrcPic(dst, view, 450);

		//cvtColor(view, dst, CV_YUV420sp2RGBA);
		//opencvIf.videoMatToHiFrame(&view, &stFrame, SAMPLE_VO_DEV_DSD1, 3);
		VIDEO_FRAME_INFO_S dststFrame;

		cout << "enPixelFormat:" << stFrame.stVFrame.enPixelFormat << endl;
		cout << "u32Width:" << stFrame.stVFrame.u32Width << endl;
		cout << "u32Height:" << stFrame.stVFrame.u32Height << endl;
		cout << "u32PhyAddr:" << stFrame.stVFrame.u32PhyAddr[0] << endl;
		cout << "u32PhyAddr:" << stFrame.stVFrame.u32PhyAddr[1] << endl;
		cout << "u32PhyAddr:" << stFrame.stVFrame.u32PhyAddr[2] << endl;

		cout << "u32Stride:" << stFrame.stVFrame.u32Stride[0] << endl;
		cout << "u32Stride:" << stFrame.stVFrame.u32Stride[1] << endl;
		cout << "u32Stride:" << stFrame.stVFrame.u32Stride[2] << endl;

		opencvIf.videoToHiFrame(&stFrame, &dststFrame, SAMPLE_VO_DEV_DSD1, 3);
		HI_MPI_VPSS_UserReleaseFrame(0, 0, &stFrame);
/*

		// 查找十字交叉点
		// 转为灰度图
		cvtColor(view, view, CV_RGB2GRAY);

		Mat op;
		mvPub.findCrossPoints(view, op);
*/
	}

	FreeMonitor(iMonitorID);

	return THREAD_RET_OK;
}

